package VendingMachine;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class VendingMachineTest {
    VendingMachine vm;
	VendingMachineItem item1;
	VendingMachineItem item2;
	VendingMachineItem item3;
	VendingMachineItem item4;

	
	@Before
	public void setUp() throws Exception {
		vm =new VendingMachine();
		item1 = new VendingMachineItem("Apple",2.00);
		item2 = new VendingMachineItem("Kitkat",2.00);
		item3 = new VendingMachineItem("Pepsi",2.00);
		item4 = new VendingMachineItem("Twix",2.00);

	}
	
	@Test
	public void AddItemTest() {
		//adding Item in the vending machine
		vm.addItem( item1,VendingMachine.A_CODE);
		assertEquals(item1, vm.getItem(VendingMachine.A_CODE ));
		vm.addItem( item2,VendingMachine.B_CODE);
		assertEquals(item2, vm.getItem(VendingMachine.B_CODE ));
		vm.addItem( item3,VendingMachine.C_CODE);
		assertEquals(item3, vm.getItem(VendingMachine.C_CODE ));
		vm.addItem( item4,VendingMachine.D_CODE);
		assertEquals(item4, vm.getItem(VendingMachine.D_CODE ));
	}
	
	@Test
	public void testAddItemInvalidcode() {
		try{
			vm.addItem( item1,"Twix");	
		}
		catch (VendingMachineException e){		
		}
	}
	
@Test
	public void testAddItemInvalidValue() {
		try{
			vm.addItem( item1,VendingMachine.A_CODE);
			vm.addItem( item3,VendingMachine.A_CODE);
			
		}
		catch (VendingMachineException e){
			
		}
	}


	
	@Test
	public void insertMoneyTest(){
	vm.addItem(item1,VendingMachine.A_CODE);
	vm.insertMoney(2.00);
     assertEquals(2.00, vm.getBalance(), 2.00);  
	}
	
	@Test
	  public void testInsertMoneyLessThanZero()

	    {
			try {
				vm.addItem(item1,VendingMachine.A_CODE);
				vm.insertMoney(-2.0);
	    }catch (VendingMachineException e){

	    }

	}
	
	@Test
	  public void testInvalidRemoveItem()

	    {
			try {
				vm.removeItem(VendingMachine.A_CODE); 
	    }catch (VendingMachineException e){

	    }

	}
	
	@Test
	public void getBalanceTest()
	{
		vm.balance=4;
         assertEquals(4,vm.getBalance(),0.01);                
	}
	
	
	@Test
	public void removeItemTest(){
		vm.addItem(item1,VendingMachine.A_CODE);
		vm.removeItem(VendingMachine.A_CODE);    
		assertEquals(vm.getItem(VendingMachine.A_CODE ),null); 
		
	}
		
	
	
	@Test
	public void makePurchaseTest()
	{
		vm.addItem(item1,VendingMachine.A_CODE);
		vm.balance=2.0;
        assertEquals(vm.makePurchase(VendingMachine.A_CODE),true);
	}
	
	@Test
	public void returnChangeTest()
	{
		vm.insertMoney(3.00);   
        assertEquals(3.00,vm.returnChange(),0.01);

	}
	
	

}
